package com.situ.mall.service.Impl;

import com.situ.mall.mapper.CartMapper;
import com.situ.mall.mapper.CouponMapper;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.entity.Coupon;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.pojo.query.CartQuery;
import com.situ.mall.pojo.query.CouponQuery;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class CouponServiceImpl implements ICouponService {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void add(Coupon coupon) {
        if (Objects.equals(coupon.getCouponName(), "100减99优惠券")) {
            coupon.setRebate(BigDecimal.valueOf(100));
            coupon.setDiscount(BigDecimal.valueOf(-99));
            coupon.setIsDeleted((byte) 0);
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            Date date1 = calendar.getTime();
            coupon.setGainDate(date);
            coupon.setExpiryDate(date1);
            couponMapper.insertSelective(coupon);
        } else if (Objects.equals(coupon.getCouponName(), "39减10优惠券")) {
            coupon.setRebate(BigDecimal.valueOf(39));
            coupon.setDiscount(BigDecimal.valueOf(-10));
            coupon.setIsDeleted((byte) 0);
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            Date date1 = calendar.getTime();
            coupon.setGainDate(date);
            coupon.setExpiryDate(date1);
            couponMapper.insertSelective(coupon);
        } else if (Objects.equals(coupon.getCouponName(), "59减15优惠券")) {
            coupon.setRebate(BigDecimal.valueOf(59));
            coupon.setDiscount(BigDecimal.valueOf(-15));
            coupon.setIsDeleted((byte) 0);
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, 7);
            Date date1 = calendar.getTime();
            coupon.setGainDate(date);
            coupon.setExpiryDate(date1);
            couponMapper.insertSelective(coupon);
        }
    }

    @Override
    public List<Coupon> selectByUserId(Integer userId) {
        CouponQuery couponQuery = new CouponQuery();
        couponQuery.setUserId(userId);
        Date date = new Date();
        couponQuery.setExpiryDate(date);
        //整个订单总金额
        BigDecimal payment = BigDecimal.valueOf(0.0);
        //查找这个用户购物车里面勾选的要结算的商品
        CartQuery cartQuery = new CartQuery();
        cartQuery.setUserId(userId);
        cartQuery.setChecked(1);
        List<CartVO> cartVOList = cartMapper.list(cartQuery);
        for (CartVO cartVO : cartVOList) {
            //计算当前商品的：价格*数量
            BigDecimal productPrice = cartVO.getPrice();
            BigDecimal quantity = BigDecimal.valueOf(cartVO.getQuantity());
            BigDecimal totalPrice = productPrice.multiply(quantity);
            payment = payment.add(totalPrice);
        }
        couponQuery.setRebate(payment);
        return couponMapper.selectByUserId(couponQuery);

    }

    @Override
    public Coupon selectById(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateCoupon(Coupon coupon) {

        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public List<Coupon> couponListSelectByUserId(Integer userId) {
        return couponMapper.couponListSelectByUserId(userId);
    }

    @Override
    public List<Coupon> selectByUserIdNowBuy(Integer userId, Integer productId, Integer quantity) {
        CouponQuery couponQuery = new CouponQuery();
        couponQuery.setUserId(userId);
        Date date = new Date();
        couponQuery.setExpiryDate(date);
        //整个订单总金额
        BigDecimal payment = BigDecimal.valueOf(0.0);
        //查找这个用户购物车里面勾选的要结算的商品
        Product product = productMapper.selectByPrimaryKey(productId);
        BigDecimal productPrice = product.getPrice();
        BigDecimal productQuantity = BigDecimal.valueOf(quantity);
        BigDecimal totalPrice = productPrice.multiply(productQuantity);
        payment = payment.add(totalPrice);
        couponQuery.setRebate(payment);
        return couponMapper.selectByUserId(couponQuery);
    }
}
